package com.example.neusoft215gdh.androidpro_21.Adapter;


import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.neusoft215gdh.androidpro_21.Activity.ShaituDetialActivity;
import com.example.neusoft215gdh.androidpro_21.JavaBean.Shaitu;
import com.example.neusoft215gdh.androidpro_21.R;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;


public class ShaituAdapter extends RecyclerView.Adapter    {


    private List<Shaitu> shaitus = new ArrayList<>();
    private Context context;//上下文对象
    private LayoutInflater layoutInflater;
    String  username;

    //构造函数
    public ShaituAdapter(Context context, List<Shaitu> shaitus ){


        this.context=context;//保存上下文
        layoutInflater=LayoutInflater.from(context);
        this.shaitus.addAll(shaitus);//显示数据

    }




    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View itemView = layoutInflater.inflate(R.layout.item_saitu,parent,false);

        SaituVierHolder holder=new SaituVierHolder(itemView);

        return holder;
    }
    //绑定数据
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        SaituVierHolder saituVierHolder = (SaituVierHolder) holder;
        Shaitu shaitu  = shaitus.get(position);
        //获取具体的数据值
        Object clubname=shaitu.getClub_name();
        String Intro =shaitu.getTitle();



        if(shaitu.getShaier()!=null) {
            username= shaitu.getShaier().getUser_name();
        }

        String commentNumber=shaitu.getHits();

        String imageview =shaitu.getThumb0();
        final String shaitu_id=shaitu.getId();

        saituVierHolder.intro.setText(Intro);
        saituVierHolder.saituclub.setText(clubname+"");
        saituVierHolder.comentnumber.setText(commentNumber);

        if(username != "") {
            saituVierHolder.username.setText(username);
        }else {


            saituVierHolder.username.setText("杨波");

        }

        if (imageview != "") {
            Picasso.with(context).load("http://172.24.10.175/workout/Uploads/"+imageview).error(R.drawable.yundong1)
                    .into(saituVierHolder.image_saitu);
        }


        if (shaitu.getShaier() != null) {
            Picasso.with(context).load("http://172.24.10.175/workout/Uploads/"+shaitu.getShaier().getPhoto()).error(R.drawable.a)
                    .into(saituVierHolder.imageView_userpic);
        }


        //页面跳转
        saituVierHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, ShaituDetialActivity.class);
                //传值
                intent.putExtra("Shaitu_id",shaitu_id);
                //在适配器里调用StarActivity方法
                context.startActivity(intent);
            }
        });

    }

    //显示条目的数量
    @Override
    public int getItemCount() {
        return shaitus.size();
    }



}
